package com.caisejingyu.whales.home.widget

import android.content.Context
import android.os.Handler
import android.os.Looper
import android.util.AttributeSet
import android.view.View
import android.widget.TextView
import com.caisejingyu.whales.home.R

/**
 * 计时textView
 */
class TimingTextView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : TextView(context, attrs, defStyleAttr) {

    constructor(context: Context) : this(context, null, 0)

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    private var timing: Int = 0
    var msg: String = ""
    private var now = 0
    private val hand: Handler = Handler(Looper.getMainLooper())
    private var timingVisible = true
    private var timingPosition = 1
    private var timingParenthesisVisible = true

    init {
        if (attrs != null) {
            val mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.TimingTextView)
            msg = mTypedArray.getString(R.styleable.TimingTextView_msg) ?: ""
            timing = mTypedArray.getInt(R.styleable.TimingTextView_timing, 0)
            timingVisible = mTypedArray.getBoolean(R.styleable.TimingTextView_timing_visiable, true)
            timingPosition = mTypedArray.getInt(R.styleable.TimingTextView_timing_parenthesis_visible, timingPosition)
            timingParenthesisVisible = mTypedArray.getBoolean(R.styleable.TimingTextView_timing_parenthesis_visible, timingParenthesisVisible)
            now = timing
            text = getText(msg, now)
            mTypedArray.recycle()
        }
    }

    fun startTiming(tim: Int, exit: () -> Unit) {
        text = getText(msg, tim)
        hand.removeCallbacksAndMessages(null)
        hand.postDelayed({
            now = tim
            if (now-- > 0) {
                text = getText(msg, now)
                startTiming(now, exit)
            } else {
                now = timing
                text = getText(msg, now)
                exit()
            }
        }, 1000)
    }

    private fun getText(msg: String, now: Int): String {
        return if (timingVisible) {
            if (timingParenthesisVisible) {
                if (timingPosition == 0) {
                    "(${now}s)$msg"
                } else {
                    "$msg(${now}s)"
                }
            } else {
                if (timingPosition == 0) {
                    "${now}s$msg"
                } else {
                    "$msg${now}s"
                }
            }
        } else {
            "$msg"
        }
    }

    fun resetMessage(message: String) {
        msg = message
        text = getText(msg, now)
    }

    fun resetMessageWithNoTime(message: String) {
        msg = message
        text = "$msg"
    }

    fun setClickListener(listener: (View) -> Unit) {
        setOnClickListener {
            listener(this)
            hand.removeCallbacksAndMessages(null)
            now = timing
            text = "$msg(${now}s)"
        }
    }

    fun removeCallback() {
        hand.removeCallbacksAndMessages(null)
    }
}